package cn.butcher.service.impl;

import cn.butcher.mapper.TeacherMapper;
import cn.butcher.service.TeacherService;
import cn.butcher.utils.SendMsg;
import cn.butcher.vo.StudentLeaveMsg;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class TeacherServiceImpl implements TeacherService {

    @Autowired
    TeacherMapper mapper;

    @Override
    public List<StudentLeaveMsg> getList(int teaId) {
        return mapper.getList(teaId);
    }

    @Override
    public int changeStatus(int leId, char status, String mark) {
        int res = mapper.changeStatus(leId, status, mark);
        Map<String, String> msg = mapper.getStudentLeaveMsg(leId);
        // 说明辅导员已同意，请假通过
        String during = msg.get("during");
        during = during.replace("2021-", "");
        during = during.replace("2020-", "");
        String[] phones = {"+86" + msg.get("phone")};

        String[] temps = {msg.get("name"), during};

        if (res >= 1) {
            try {
                if (status == '3') {
                    SendMsg.send("1046801", phones, temps);
                } else if (status == '4' || status == '2') {
                    // 说明辅导员不同意或任课老师不通过
                    temps = new String[]{msg.get("name"), mark};
                    SendMsg.send("1058914", phones, temps);
                }
            } catch (TencentCloudSDKException e) {
                e.printStackTrace();
            }
        }
        return res;
    }

    @Override
    public List<StudentLeaveMsg> getList2(int teId) {
        return mapper.getList2(teId);
    }
}
